﻿using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using gms.system;
using NHibernate.Criterion;

namespace gms.data {
	public class Player_Aclgroup {
		public static IList<Acl_Group> get_groups_of(int id) {
			NHDatabase nhdb = new NHDatabase();
			DataSet values = Database.exec_ds_return("gms.pr_player_acl_group_load_groups", new SqlParameter("player_id", id));
			IList<Acl_Group> retval = null;

			if (!Database.dataset_is_empty(values)) {
				int[] ids = new int[values.Tables[0].Rows.Count];

				for (int i = 0; i < ids.Length; i++) {
					ids[i] = (int)values.Tables[0].Rows[i]["acl_id"];
				}

				retval = nhdb.session.CreateCriteria<Acl_Group>().Add(Restrictions.In("acl_id", ids)).List<Acl_Group>();
			}

			nhdb.Dispose();

			return retval;
		}

		public static void delete_by_player(int id) {
			Database.exec("gms.pr_player_acl_group_delete_player", new SqlParameter("player_id", id));
		}

		public static void delete_by_group(int id) {
			Database.exec("gms.pr_player_acl_group_delete_group", new SqlParameter("acl_id", id));
		}

		public static void create(int player_id, int acl_id) {
			Database.exec("gms.pr_player_acl_group_add", new SqlParameter("player_id", player_id), new SqlParameter("acl_id", acl_id));
		}
	}
}